package com.qf.myshop.dao.impl;

import com.qf.myshop.dao.TypeDAO;
import com.qf.myshop.entity.Type;
import com.qf.myshop.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TypeDAOImpl implements TypeDAO {
    private QueryRunner queryRunner = new QueryRunner();

    @Override
    public List<Type> findAll() throws SQLException {
        return queryRunner.query(JDBCUtils.getConnection(),
                "select * from `type`",
                new ResultSetHandler<List<Type>>() {
            @Override
            public List<Type> handle(ResultSet rs) throws SQLException {
                List<Type> list = new ArrayList<>();
                while (rs.next()){
                    list.add(new Type(
                            rs.getInt("t_id"),
                            rs.getString("t_name")
                    ));
                }
                return list;
            }
        });
    }
}
